﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;//添加引用的线程类

namespace _04_Thread线程基础
{
    public partial class Form1 : Form
    {
        //线程类对象
        Thread thread = null;
        
        public Form1()
        {
            InitializeComponent();
            //取消线程的安全检查
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        /// <summary>
        /// 启动线程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //实例化委托
            ThreadStart threadStart = new ThreadStart(DoWrok);
            //创建线程对象
            thread = new Thread(threadStart);
            //设置线程为后台线程
            thread.IsBackground = true;
            //启动线程
            thread.Start();
        }
        /// <summary>
        /// 线程要执行的方法
        /// </summary>
        public  void DoWrok()
        {
            for (int i = 0; i <= 100; i++)
            {
                this.progressBar1.Value = i;//调整进度条的值
                this.label2.Text = i.ToString() + "%";
                Thread.Sleep(50);
            }
        }
        /// <summary>
        /// 线程中断
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            thread.Interrupt();
        }
        /// <summary>
        /// 线程挂起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            thread.Suspend();
        }
        /// <summary>
        /// 线程唤醒
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            thread.Resume();
        }
        /// <summary>
        /// 线程终止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            thread.Abort();
        }
    }
}
